<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\base\Exception;
use common\models\CheckUser;

class OrderForm extends Model
{
    public $id;
    public $barcode;
    public $name;
    public $gender;
    public $mobileNumber;
    public $samplingDate;

	private $_order;

	public function formName()
    {
        return '';
    }

	public function rules()
	{
		return [
            [['name', 'gender', 'mobileNumber', 'samplingDate'], 'required'],
            ['barcode', 'safe']
		];
	}

    public function __construct($id = null, $config = []) {
        if (!empty($id)) {
            $order = CheckUser::findOne($id);
            $this->id = $order->id;
            $this->name = $order->name;
            $this->gender = $order->gender;
            $this->mobileNumber = $order->mobileNumber;
            $this->samplingDate = $order->samplingDate;
            if ($order->barcode) {
                $this->barcode = $order->barcode;
            }
        } else {
            $order = new CheckUser();
        }
        $this->_order = $order;

        return parent::__construct($config);
    }

    public function addOrUpdate()
    {
        $order = $this->_order;
        $order->name = $this->name;
        $order->gender = $this->gender;
        $order->mobileNumber = $this->mobileNumber;
        $order->samplingDate = strtotime($this->samplingDate);
        if ($this->barcode) {
            $order->barcode = $this->barcode;
        }

        if ($order->save(false)) {

            return true;
        } else {

            return false;
        }
    }

}    